﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BadHabit.Factory.Reports {

    public class ProjectPerformanceReport : Report  {


        Data.ProjectPerformanceDataSetTableAdapters.ProjectPerformanceTableAdapter performanceTA = new BadHabit.Factory.Data.ProjectPerformanceDataSetTableAdapters.ProjectPerformanceTableAdapter();
        Data.ProjectPerformanceDataSetTableAdapters.ProjectRoleTableAdapter performanceRoleTA = new BadHabit.Factory.Data.ProjectPerformanceDataSetTableAdapters.ProjectRoleTableAdapter();

        private int year;
        private string member = null;

        public ProjectPerformanceReport(Factory factory, int year, string member) : base(factory) {
            this.year = year;
            this.member = member;
        }

        public ProjectPerformanceReport(Factory factory, int year) : base(factory) {
            this.year = year;            
        }

        

        /// <summary>
        /// The template type name.
        /// </summary>
        public override string TemplateTypeName {
            get {
                return "ProjectPerformanceReport";
            }
        }

        /// <summary>
        /// Adjusts the dataset for XPS render.
        /// </summary>
        /// <param name="projectUId">The projectUId</param>
        /// <returns>The adjusted dataset</returns>
        public override System.Data.DataSet CreateDataSet() {
            Data.ProjectPerformanceDataSet ds = new BadHabit.Factory.Data.ProjectPerformanceDataSet();
            if (member == null) 
                performanceTA.FillByYear(ds.ProjectPerformance, year);
            else
                performanceTA.FillByYearAndMember(ds.ProjectPerformance, year, member);

            performanceRoleTA.FillByYear(ds.ProjectRole, year);

            
            return ds;
        }


        public override System.Data.DataSet AdjustDataSet(System.Data.DataSet report) {

            report = base.AdjustDataSet(report);
            report.Tables["General"].Columns.Add("SelectedYear", typeof(int));
            report.Tables["General"].Columns.Add("SelectedMember", typeof(string));
            report.Tables["General"].Columns.Add("SelectedMemberName", typeof(string));
            report.Tables["General"].Rows[0]["SelectedYear"] = year;
            if (member != null) {
                report.Tables["General"].Rows[0]["SelectedMember"] = member;
                Data.TeamDataSet.MemberRow memberRow = factory.FindTeamMember(member);
                if (memberRow != null) 
                    report.Tables["General"].Rows[0]["SelectedMemberName"] = memberRow.NickName;                
            }

            report.Tables["ProjectPerformance"].Columns.Add("StartDateString", typeof(string));
            report.Tables["ProjectPerformance"].Columns.Add("EndDateString", typeof(string));
            report.Tables["ProjectPerformance"].Columns.Add("EstimatedEndDateString", typeof(string));
            foreach (Data.ProjectPerformanceDataSet.ProjectPerformanceRow p in report.Tables["ProjectPerformance"].Rows) {
                if(!p.IsStartDateNull())
                    p["StartDateString"] = p.StartDate.ToShortDateString();
                if (!p.IsEndDateNull())
                    p["EndDateString"] = p.EndDate.ToShortDateString();
                p["EstimatedEndDateString"] = p.EstimatedEndDate.ToShortDateString();
            }


            return report;
        }
    }
}
